package com.gee.spring.cloud.feign.flow.binding;

import com.gee.spring.cloud.feign.flow.util.FlowUtil;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.FactoryBean;

import java.util.concurrent.ThreadPoolExecutor;

/**
 * desc:
 *
 * @author gee
 * @since 2021-03-31 15:39:08
 */
public class FeignFlowFactoryBean<T> implements FactoryBean<T> {

    private final Class<T> feignFlowInterface;
    private BeanFactory beanFactory;
    private ThreadPoolExecutor feignFlowThreadPoolExecutor;
    public FeignFlowFactoryBean(Class<T> feignFlowInterface, BeanFactory beanFactory) {
        this.feignFlowInterface = feignFlowInterface;
        this.beanFactory = beanFactory;
    }

    @Override
    public T getObject() throws Exception {
        if (!FlowUtil.hasFeignFlow(feignFlowInterface)){
            FlowUtil.addFeignFlow(feignFlowInterface,beanFactory,feignFlowThreadPoolExecutor);
        }
        return FlowUtil.getFeignFlow(feignFlowInterface);
    }

    @Override
    public Class<T> getObjectType() {
        return feignFlowInterface;
    }

    public void setFeignFlowThreadPoolExecutor(ThreadPoolExecutor feignFlowThreadPoolExecutor) {
        this.feignFlowThreadPoolExecutor = feignFlowThreadPoolExecutor;
    }

}
